package com.example.appointment.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.appointment.R;
import com.example.appointment.adapter.ChooseProAdapter;
import com.example.appointment.meta.CitiesBean;
import com.example.appointment.meta.DistrictsBean;
import com.example.appointment.meta.ProvincesBean;
import com.example.appointment.util.Constant;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * 选择省份的界面
 */
public class ChooseAreaActivity extends BaseActivity {
    @Bind(R.id.activity_chooser_arealist)
    RecyclerView chooserarealist;
    private ArrayList<CitiesBean> citiesBean;//城市的对象
    private ChooseProAdapter adapter;
    private ProvincesBean itemvo;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ONITEM_CLICK:
                    //列表的点击事件
                    itemvo = (ProvincesBean) msg.obj;
                    citiesBean = (ArrayList<CitiesBean>) DataSupport.where("ProID is ?", itemvo.getProID() + "").find(CitiesBean.class);
                    startActivityForResult(new Intent(ChooseAreaActivity.this, ChooseCityActivity.class).putExtra("citylist", citiesBean), 0x111);

                    break;

            }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0x111:
                    final DistrictsBean districtsbean = (DistrictsBean) data.getSerializableExtra("dis");
                    final CitiesBean citiesBean = (CitiesBean) data.getSerializableExtra("city");

//                    citiesBean = (ArrayList<CitiesBean>) DataSupport.where("ProID is ?", itemvo.getProID() + "").find(CitiesBean.class);
//                    Toast.makeText(ChooseAreaActivity.this, itemvo.getName() + " " + citiesBean.getName() + " " + districtsbean.getDisName(), Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.putExtra("area", itemvo.getName() + " " + citiesBean.getName() + " " + districtsbean.getDisName());
                    setResult(RESULT_OK, intent);
                    ChooseAreaActivity.this.finish();
//                    signature
                    break;
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setnotificationColore(R.color.maincolor);//設置导航栏顔色
        setContentView(R.layout.activity_choosearea);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        chooserarealist.setLayoutManager(layoutManager);

        adapter = new ChooseProAdapter(ChooseAreaActivity.this, mHandler);
        ArrayList<ProvincesBean> provincesbean = (ArrayList<ProvincesBean>) DataSupport.findAll(ProvincesBean.class);

        adapter.setArrayList(provincesbean);
//        loadToast = new LoadToast(SaveCarMaActivity.this);
        chooserarealist.setAdapter(adapter);

//        * 查找所有地区
//        cityVO = new CityVO();
//        List<CitiesBean> citiesbean = DataSupport.findAll(CitiesBean.class);
//        List<DistrictsBean> districtsbean = DataSupport.findAll(DistrictsBean.class);

////        Log.d("iiiiiiiis", "onCreate: " + allcity.size() + allcity.toString());
//        cityVO.setCities(citiesbean);
//        cityVO.setDistricts(districtsbean);
//        cityVO.setProvinces(provincesbean);

        initView();

    }

    /**
     *
     */
    private void initView() {
        setTitile("选择省份");
//        testtext.setText(cityVO.toString());
    }


}
